package com.ruoyi.tb.lmt.controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.tb.lmt.domain.bo.*;
import com.ruoyi.tb.lmt.domain.vo.TbMutualInspectiondetailVo;
import com.ruoyi.tb.lmt.service.*;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbMutualInspectionVo;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 互检主表
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/mutualInspection")
public class TbMutualInspectionController extends BaseController {

    private final ITbMutualInspectionService iTbMutualInspectionService;

    @Autowired
    private ITbRepairplanlistService iTbRepairplanlistService;

    @Autowired
    private ITbLmtrepairWorkdetailService iTbLmtrepairWorkdetailService;

    @Autowired
    private ITbSpecialInspectionService iTbSpecialInspectionService;

    /**
     * 查询互检主表列表
     */
    @GetMapping("/list")
    public TableDataInfo<TbMutualInspectionVo> list(TbMutualInspectionBo bo, PageQuery pageQuery) {
        return iTbMutualInspectionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出互检主表列表
     */
    @Log(title = "互检主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbMutualInspectionBo bo, HttpServletResponse response) {
        List<TbMutualInspectionVo> list = iTbMutualInspectionService.queryList(bo);
        ExcelUtil.exportExcel(list, "互检主表", TbMutualInspectionVo.class, response);
    }

    /**
     * 获取互检主表详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<TbMutualInspectionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTbMutualInspectionService.queryById(id));
    }

    /**
     * 新增互检主表
     */
    @SaCheckPermission("xha-lmt:mutualInspection:add")
    @Log(title = "互检主表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody List<TbMutualInspectionBo> boList) {
        for (TbMutualInspectionBo bo : boList) {
            TbMutualInspectionBo bos = iTbMutualInspectionService.selectByBo(bo);
            iTbMutualInspectionService.insertByBo(bos);

        }
        return toAjax(boList.size());
    }

    /**
     * 修改互检主表
     */
    @SaCheckPermission("xha-lmt:mutualInspection:edit")
    @Log(title = "互检主表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbMutualInspectionBo bo) {
        return toAjax(iTbMutualInspectionService.updateByBo(bo));
    }

    /**
     * 删除互检主表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xha-lmt:mutualInspection:remove")
    @Log(title = "互检主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTbMutualInspectionService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    //查询互检主表
    @GetMapping("/list/selectByMutual")
    public TableDataInfo<TbMutualInspectionVo> selectByMutual(String id, String lmtType, String lmtNumber, String status, String repairChar, String planName, String workGroup, String workUser, PageQuery pageQuery) {
        return iTbMutualInspectionService.selectByMutual(id,lmtType,lmtNumber,status,repairChar,planName,workGroup,workUser,pageQuery);
    }


    /**
     * 审批设置互检工队，要设置权限
     */
    @SaCheckPermission("xha-lmt:mutualInspection:updateDispatch")
    @Log(title = "审批互检主表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("update/dispatch")
    public R<Void> updateDispatch(@Validated(EditGroup.class) @RequestBody TbMutualInspectionBo bo) {
        bo.setStatus("待派活");
        TbRepairplanlistBo rplBo = new TbRepairplanlistBo();
        rplBo.setId(bo.getLmtRepairId());
        rplBo.setStatus("互检中");
        iTbRepairplanlistService.updateByBo(rplBo);
        return toAjax(iTbMutualInspectionService.updateByBo(bo));
    }

    /**
     * 设置互检人员
     */
    @SaCheckPermission("xha-lmt:mutualInspection:updateUser")
    @Log(title = "设置互检人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("update/user")
    public R<Void> updateUser(@Validated(EditGroup.class) @RequestBody TbMutualInspectionBo bo) {
        bo.setStatus("互检中");
        bo.setSendToUserTime(new Date());
        return toAjax(iTbMutualInspectionService.updateByBo(bo));
    }

    /**
     * 设置互检通过
     */
    @SaCheckPermission("xha-lmt:mutualInspection:updateApproval")
    @Log(title = "互检通过", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("update/approval")
    public R<Void> updateApproval(@Validated(EditGroup.class) @RequestBody TbMutualInspectionBo bo) {
        bo.setStatus("已完成");
        bo.setWorkUser(LoginHelper.getUsername());
        bo.setWorkGroupId(LoginHelper.getDeptId());
        bo.setWorkGroup(LoginHelper.getLoginUser().getDeptName());
        TbRepairplanlistBo rplBo = new TbRepairplanlistBo();
        rplBo.setId(bo.getLmtRepairId());
        rplBo.setStatus("互检完成");
        iTbRepairplanlistService.updateByBo(rplBo);

        //设置检修中的互检任务完成
        Long lmtRepairId = bo.getLmtRepairId();
        TbLmtrepairWorkdetailBo lmtBo= iTbLmtrepairWorkdetailService.selectByMutual(lmtRepairId);
        if(lmtBo != null) {
            //查询互检问题，并且设置完成
            lmtBo.setStatus("已完成");
            lmtBo.setEndTime(new Date());
            iTbLmtrepairWorkdetailService.updateByBo(lmtBo);
        }
        //当互检通过时，生成专检表数据
        TbSpecialInspectionBo bos = new TbSpecialInspectionBo();
        bos.setLmtRepairId(bo.getLmtRepairId());
        bos.setPlanId(bo.getPlanId());
        bos.setPlanName(bo.getPlanName());
        bos.setLmtType(bo.getLmtType());
        bos.setLmtNumber(bo.getLmtNumber());
        bos.setWorkerTeam(bo.getWorkerTeam());
        bos.setWorkGroup(bo.getWorkGroup());
        bos.setWorkGroupId(bo.getWorkGroupId());
        bos.setRepairChar(bo.getRepairChar());
        bos.setRepairType(bo.getRepairType());
        bos.setRepairTimes(bo.getRepairTimes());
        bos.setRepairCode(bo.getRepairCode());
        bos.setRepairStartDate(bo.getRepairStartDate());
        bos.setStatus("专检中");
        //设置完实体类，调用互检新增方法
        TbSpecialInspectionBo bo1 = iTbSpecialInspectionService.selectByBo(bos);
        iTbSpecialInspectionService.insertByBo(bo1);

        return toAjax(iTbMutualInspectionService.updateByBo(bo));
    }
    @Log(title = "修改互检问题数", businessType = BusinessType.UPDATE)
    @PutMapping("update/workNeed")
    public R<Void> updateWorkNeed(@Validated(EditGroup.class) @RequestBody TbMutualInspectiondetailBo bo) {
        //service层处理传入的bo数据
        TbMutualInspectionBo bos = new TbMutualInspectionBo();
        bos.setId(bo.getMutualId());
        bos.setWorkNeed(iTbMutualInspectionService.updateWorkNeedByBo(bo.getMutualId()));
        return toAjax(iTbMutualInspectionService.updateByBo(bos));
    }
}
